require('dotenv').config({ path: './nestjs-app/.env' });
const mysql = require('mysql2/promise');

// 角色升级经验数据（从图片中解析）
const levelExperienceData = [
    { level: 1, experience: 100 },
    { level: 2, experience: 220 },
    { level: 3, experience: 360 },
    { level: 4, experience: 520 },
    { level: 5, experience: 700 },
    { level: 6, experience: 900 },
    { level: 7, experience: 1120 },
    { level: 8, experience: 1360 },
    { level: 9, experience: 1620 },
    { level: 10, experience: 1900 },
    { level: 11, experience: 2200 },
    { level: 12, experience: 2520 },
    { level: 13, experience: 2860 },
    { level: 14, experience: 3220 },
    { level: 15, experience: 3600 },
    { level: 16, experience: 4000 },
    { level: 17, experience: 4420 },
    { level: 18, experience: 4860 },
    { level: 19, experience: 5320 },
    { level: 20, experience: 5800 },
    { level: 21, experience: 6300 },
    { level: 22, experience: 6820 },
    { level: 23, experience: 7360 },
    { level: 24, experience: 7920 },
    { level: 25, experience: 8500 },
    { level: 26, experience: 9100 },
    { level: 27, experience: 9720 },
    { level: 28, experience: 10360 },
    { level: 29, experience: 11020 },
    { level: 30, experience: 11700 },
    { level: 31, experience: 12400 },
    { level: 32, experience: 13120 },
    { level: 33, experience: 13860 },
    { level: 34, experience: 14620 },
    { level: 35, experience: 15400 },
    { level: 36, experience: 16200 },
    { level: 37, experience: 17020 },
    { level: 38, experience: 17860 },
    { level: 39, experience: 18720 },
    { level: 40, experience: 19600 },
    { level: 41, experience: 20500 },
    { level: 42, experience: 21420 },
    { level: 43, experience: 22360 },
    { level: 44, experience: 23320 },
    { level: 45, experience: 24300 },
    { level: 46, experience: 25300 },
    { level: 47, experience: 26320 },
    { level: 48, experience: 27360 },
    { level: 49, experience: 28420 },
    { level: 50, experience: 29500 },
    { level: 51, experience: 30600 },
    { level: 52, experience: 31720 },
    { level: 53, experience: 32860 },
    { level: 54, experience: 34020 },
    { level: 55, experience: 35200 },
    { level: 56, experience: 36400 },
    { level: 57, experience: 37620 },
    { level: 58, experience: 38860 },
    { level: 59, experience: 40120 },
    { level: 60, experience: 41400 },
    { level: 61, experience: 42700 },
    { level: 62, experience: 44020 },
    { level: 63, experience: 45360 },
    { level: 64, experience: 46720 },
    { level: 65, experience: 48100 },
    { level: 66, experience: 49500 },
    { level: 67, experience: 50920 },
    { level: 68, experience: 52360 },
    { level: 69, experience: 53820 },
    { level: 70, experience: 55300 },
    { level: 71, experience: 56800 },
    { level: 72, experience: 58320 },
    { level: 73, experience: 59860 },
    { level: 74, experience: 61420 },
    { level: 75, experience: 63000 },
    { level: 76, experience: 64600 },
    { level: 77, experience: 66220 },
    { level: 78, experience: 67860 },
    { level: 79, experience: 69520 },
    { level: 80, experience: 71200 },
    { level: 81, experience: 72900 },
    { level: 82, experience: 74620 },
    { level: 83, experience: 76360 },
    { level: 84, experience: 78120 },
    { level: 85, experience: 79900 },
    { level: 86, experience: 81700 },
    { level: 87, experience: 83520 },
    { level: 88, experience: 85360 },
    { level: 89, experience: 87220 },
    { level: 90, experience: 89100 },
    { level: 91, experience: 91000 },
    { level: 92, experience: 92920 },
    { level: 93, experience: 94860 },
    { level: 94, experience: 96820 },
    { level: 95, experience: 98800 },
    { level: 96, experience: 100800 },
    { level: 97, experience: 102820 },
    { level: 98, experience: 104860 },
    { level: 99, experience: 106920 },
    { level: 100, experience: 109000 },
    { level: 101, experience: 111100 },
    { level: 102, experience: 113220 },
    { level: 103, experience: 115360 },
    { level: 104, experience: 117520 },
    { level: 105, experience: 119700 },
    { level: 106, experience: 121900 },
    { level: 107, experience: 124120 },
    { level: 108, experience: 126360 },
    { level: 109, experience: 128620 },
    { level: 110, experience: 130900 },
    { level: 111, experience: 133200 },
    { level: 112, experience: 135520 },
    { level: 113, experience: 137860 },
    { level: 114, experience: 140220 },
    { level: 115, experience: 142600 },
    { level: 116, experience: 145000 },
    { level: 117, experience: 147420 },
    { level: 118, experience: 149860 },
    { level: 119, experience: 152320 },
    { level: 120, experience: 154800 },
    { level: 121, experience: 157300 },
    { level: 122, experience: 159820 },
    { level: 123, experience: 162360 },
    { level: 124, experience: 164920 },
    { level: 125, experience: 167500 },
    { level: 126, experience: 170100 },
    { level: 127, experience: 172720 },
    { level: 128, experience: 175360 },
    { level: 129, experience: 178020 },
    { level: 130, experience: 180700 },
    { level: 131, experience: 183400 },
    { level: 132, experience: 186120 },
    { level: 133, experience: 188860 },
    { level: 134, experience: 191620 },
    { level: 135, experience: 194400 },
    { level: 136, experience: 197200 },
    { level: 137, experience: 200020 },
    { level: 138, experience: 202860 },
    { level: 139, experience: 205720 },
    { level: 140, experience: 208600 },
    { level: 141, experience: 211500 },
    { level: 142, experience: 214420 },
    { level: 143, experience: 217360 },
    { level: 144, experience: 220320 },
    { level: 145, experience: 223300 },
    { level: 146, experience: 226300 },
    { level: 147, experience: 229320 },
    { level: 148, experience: 232360 },
    { level: 149, experience: 235420 },
    { level: 150, experience: 238500 },
    { level: 151, experience: 241600 },
    { level: 152, experience: 244720 },
    { level: 153, experience: 247860 },
    { level: 154, experience: 251020 },
    { level: 155, experience: 254200 },
    { level: 156, experience: 257400 },
    { level: 157, experience: 260620 },
    { level: 158, experience: 263860 },
    { level: 159, experience: 267120 }
];

async function importLevelExperienceData() {
    console.log('🚀 开始导入角色升级经验数据...');

    const connection = await mysql.createConnection({
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: process.env.DB_NAME,
        port: parseInt(process.env.DB_PORT || '3306', 10),
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0
    });

    try {
        await connection.connect();
        console.log('✅ 数据库连接成功！');

        // 检查表是否存在
        const [rows] = await connection.execute("SHOW TABLES LIKE 'level_experience'");
        if (rows.length === 0) {
            console.error('❌ level_experience 表不存在，请先创建表结构。');
            return;
        }
        console.log('✅ level_experience 表存在');

        console.log(`📊 准备导入 ${levelExperienceData.length} 个等级的经验数据`);

        // 清空现有数据
        console.log('🗑️ 清空现有角色升级经验数据...');
        await connection.execute('DELETE FROM level_experience');
        console.log('✅ 现有数据已清空');

        // 导入新数据
        console.log('📥 开始导入角色升级经验数据...');

        const insertSql = `
            INSERT INTO level_experience (level, experience, created_at, updated_at)
            VALUES (?, ?, NOW(), NOW())
        `;

        let successCount = 0;
        let errorCount = 0;

        for (const data of levelExperienceData) {
            try {
                const values = [data.level, data.experience];
                await connection.execute(insertSql, values);
                
                if (data.level % 20 === 0 || data.level <= 10) {
                    console.log(`✅ 导入等级 ${data.level}: ${data.experience.toLocaleString()} 经验`);
                }
                successCount++;

            } catch (error) {
                console.error(`❌ 导入等级 ${data.level} 失败:`, error.message);
                errorCount++;
            }
        }

        console.log('\n📊 导入结果统计:');
        console.log(`✅ 成功导入: ${successCount} 条记录`);
        console.log(`❌ 导入失败: ${errorCount} 条记录`);
        console.log(`📈 总计处理: ${levelExperienceData.length} 条记录`);

        console.log('\n🔍 验证导入结果...');
        const [countRows] = await connection.execute('SELECT COUNT(*) AS count FROM level_experience');
        console.log(`📊 数据库中现有等级经验数据: ${countRows[0].count} 条`);

        // 验证几个关键等级
        console.log('\n📋 关键等级验证:');
        const keyLevels = [1, 10, 30, 50, 80, 100, 120, 150, 159];
        for (const level of keyLevels) {
            const [result] = await connection.execute(
                'SELECT level, experience FROM level_experience WHERE level = ?',
                [level]
            );
            if (result.length > 0) {
                console.log(`  等级 ${level}: ${result[0].experience.toLocaleString()} 经验`);
            }
        }

        // 验证经验值递增
        console.log('\n📈 经验值递增验证:');
        const [increasingCheck] = await connection.execute(`
            SELECT 
                COUNT(*) as total,
                SUM(CASE WHEN experience > LAG(experience) OVER (ORDER BY level) THEN 1 ELSE 0 END) as increasing_count
            FROM level_experience
        `);
        const total = increasingCheck[0].total;
        const increasing = increasingCheck[0].increasing_count;
        console.log(`  总记录数: ${total}`);
        console.log(`  递增记录数: ${increasing}`);
        console.log(`  递增率: ${((increasing / (total - 1)) * 100).toFixed(2)}%`);

        console.log('\n🎉 角色升级经验数据导入完成！');

    } catch (error) {
        console.error('❌ 导入过程中发生错误:', error.message);
    } finally {
        await connection.end();
        console.log('🔌 数据库连接已关闭');
        console.log('✅ 脚本执行完成');
    }
}

importLevelExperienceData();
